package com.info33.platform.config;

import com.info33.platform.common.util.SysUtil;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.function.Predicate;

/**
 * EnableOpenApi 开房 swagger 3
 *
 * @author Administrator
 */
@Configuration
@EnableOpenApi
public class Swagger3Config {

    @Bean
    public Docket system(Environment environment) {
        return baseDocket(environment, RequestHandlerSelectors.basePackage("com.info33.platform.system"),"system");
    }

    @Bean
    public Docket doc(Environment environment) {
        return baseDocket(environment, RequestHandlerSelectors.basePackage("com.info33.platform.doc"),"doc");
    }

    @Bean
    public Docket ecology(Environment environment) {
        return baseDocket(environment, RequestHandlerSelectors.basePackage("com.info33.platform.business.ecology"),"ecology");
    }
    @Bean
    public Docket service(Environment environment) {
        return baseDocket(environment, RequestHandlerSelectors.basePackage("com.info33.platform.business.service"),"service");
    }
    @Bean
    public Docket management(Environment environment) {
        return baseDocket(environment, RequestHandlerSelectors.basePackage("com.info33.platform.business.management"),"management");
    }

    private Docket baseDocket(Environment environment, Predicate<RequestHandler> requestHandlerPredicate, String groupName) {
        return new Docket(DocumentationType.OAS_30)
                .enable(SysUtil.isDev(environment))
                .apiInfo(apiInfo())
                .groupName(groupName)
                .select()
                .apis(requestHandlerPredicate)
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("叁拾叁信息科技平台-后端RESTFUL APIs")
                .description("接口说明")
                .termsOfServiceUrl("")
                .contact(new Contact("33INFO", "http://www.33iot.com", "admin@33iot.com"))
                .version("1.0")
                .build();
    }

}